<template>
  <van-icon
    :color="value ? '#ffa500' : '#777'"
    :name="value ? 'star' : 'star-o'"
    @click="onCollected"
  />
</template>

<script>
import { addCollected, deleteCollected } from '@/api/article.js'
export default {
  name: 'articleColle',
  data () {
    return {

    }
  },
  props: {
    value: {
      type: Boolean,
      required: true
    },
    articleId: {
      type: [Number, String, Object],
      required: true
    }
  },
  created () {

  },

  methods: {
    async onCollected () {
      try {
        if (this.value) {
          // 已收藏 要 取消收藏
          await deleteCollected(this.articleId)
        } else {
          // 未收藏 要 添加收藏
          await addCollected(this.articleId)
        }
        this.$emit('input', !this.value)
        this.$toast.success('操作成功!')
      } catch (error) {
        this.$toast('操作失败!')
      }
    }
  }
}
</script>

<style scoped lang='less'>

</style>
